package com.mj.flowable.examples.config;

import com.mj.flowable.examples.event.TaskEventListener;
import lombok.RequiredArgsConstructor;
import org.flowable.common.engine.impl.history.HistoryLevel;
import org.flowable.common.engine.impl.persistence.StrongUuidGenerator;
import org.flowable.engine.*;
import org.flowable.engine.repository.Deployment;
import org.flowable.engine.test.profiler.FlowableProfiler;
import org.flowable.spring.ProcessEngineFactoryBean;
import org.flowable.spring.SpringProcessEngineConfiguration;
import org.flowable.spring.boot.BaseEngineConfigurationWithConfigurers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.PlatformTransactionManager;

import javax.annotation.PostConstruct;
import javax.sql.DataSource;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

@Configuration
public class HolidayRequestConfig extends BaseEngineConfigurationWithConfigurers<SpringProcessEngineConfiguration> {

    @Autowired
    protected DataSource dataSource;

    @Autowired
    protected PlatformTransactionManager transactionManager;


    @Bean
    public SpringProcessEngineConfiguration springProcessEngineConfiguration() {
        SpringProcessEngineConfiguration processEngineConfiguration = new SpringProcessEngineConfiguration();
        processEngineConfiguration.setDataSource(dataSource);
        processEngineConfiguration.setDatabaseSchemaUpdate("none");
        processEngineConfiguration.setTransactionManager(transactionManager);
        //设置ID为UUID
        processEngineConfiguration.setIdGenerator(new StrongUuidGenerator());
        //定时器
        processEngineConfiguration.setAsyncExecutorActivate(false);
//        processEngineConfiguration.setJpaHandleTransaction(false);
        processEngineConfiguration.setHistoryLevel(HistoryLevel.FULL);
        processEngineConfiguration.setEnableDatabaseEventLogging(false);
        processEngineConfiguration.setEnableEntityLinks(true);
        processEngineConfiguration.setDisableIdmEngine(true);
        processEngineConfiguration.setEnableExecutionRelationshipCounts(true);
        processEngineConfiguration.setEnableLogSqlExecutionTime(true);
        processEngineConfiguration.setEnableHistoricTaskLogging(false);
        processEngineConfiguration.setEnableTaskRelationshipCounts(false);
//        processEngineConfiguration.setEventListeners(Arrays.asList(getListener()));
        processEngineConfiguration.setActivityFontName("宋体");
        processEngineConfiguration.setLabelFontName("宋体");
        processEngineConfiguration.setAnnotationFontName("宋体");
//        Set customermapperSet = new HashSet();
//        customermapperSet.add(RestartProcessInstanceModelLogDao.class);
//        customermapperSet.add(AgentSettingDao.class);
//        processEngineConfiguration.setCustomMybatisMappers(customermapperSet);
        processEngineConfiguration.setCreateDiagramOnDeploy(false);
        FlowableProfiler flowableProfiler = new FlowableProfiler();
        processEngineConfiguration.addConfigurator(flowableProfiler);
        processEngineConfiguration.setEngineName("flowable");

        return processEngineConfiguration;
    }




    @Bean
    public RuntimeService runtimeServiceBean(ProcessEngine processEngine) {
        return processEngine.getRuntimeService();
    }

    @Bean
    public RepositoryService repositoryServiceBean(ProcessEngine processEngine) {
        return processEngine.getRepositoryService();
    }

    @Bean
    public TaskService taskServiceBean(ProcessEngine processEngine) {
        return processEngine.getTaskService();
    }

    @Bean
    public HistoryService historyServiceBean(ProcessEngine processEngine) {
        return processEngine.getHistoryService();
    }

    @Bean
    public ManagementService managementServiceBean(ProcessEngine processEngine) {
        return processEngine.getManagementService();
    }

    @Bean
    public DynamicBpmnService dynamicBpmnServiceBean(ProcessEngine processEngine) {
        return processEngine.getDynamicBpmnService();
    }

    @Bean
    public ProcessMigrationService processInstanceMigrationService(ProcessEngine processEngine) {
        return processEngine.getProcessMigrationService();
    }

//    @Bean
//    public FormService formServiceBean(ProcessEngine processEngine) {
//        return processEngine.getFormService();
//    }

    @Bean
    public IdentityService identityServiceBean(ProcessEngine processEngine) {
        return processEngine.getIdentityService();
    }

    @Bean
    public ProcessEngineFactoryBean processEngine(SpringProcessEngineConfiguration springProcessEngineConfiguration) throws Exception {
        ProcessEngineFactoryBean processEngineFactoryBean = new ProcessEngineFactoryBean();
        processEngineFactoryBean.setProcessEngineConfiguration(springProcessEngineConfiguration);
        invokeConfigurers(springProcessEngineConfiguration);
        return processEngineFactoryBean;
    }


}
